package com.ruoyi.common.utils.excl.export;

import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.utils.StringUtils;
import org.apache.poi.ss.usermodel.*;

import java.util.HashMap;
import java.util.Map;

public class CustomExcelExportStylerDefaultImpl extends ExcelExportStylerDefaultImpl {

    private CellStyle defaultNumberStyle;

    private Map<String, CellStyle> styleMap;

    public CustomExcelExportStylerDefaultImpl(Workbook workbook) {
        super(workbook);
        styleMap = new HashMap<>(4);
        defaultNumberStyle = createNumberCellStyler();
        styleMap.put("0", defaultNumberStyle);
        styleMap.put("#,##0.00", createCurrencyCellStyler());
        styleMap.put("0.00", createDecimalCellStyler());
        styleMap.put("0.00%", createPercentCellStyler());
    }

    private CellStyle createCurrencyCellStyler() {
        CellStyle currencyCellStyle = workbook.createCellStyle();
        currencyCellStyle.setAlignment(HorizontalAlignment.CENTER);
        currencyCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        currencyCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("#,##0.00"));
        currencyCellStyle.setWrapText(true);
        return currencyCellStyle;
    }

    private CellStyle createNumberCellStyler() {
        CellStyle numberCellStyle = workbook.createCellStyle();
        numberCellStyle.setAlignment(HorizontalAlignment.CENTER);
        numberCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        numberCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0"));
        numberCellStyle.setWrapText(true);
        return numberCellStyle;
    }

    private CellStyle createDecimalCellStyler() {
        CellStyle decimalCellStyle = workbook.createCellStyle();
        decimalCellStyle.setAlignment(HorizontalAlignment.CENTER);
        decimalCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        decimalCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00"));
        decimalCellStyle.setWrapText(true);
        return decimalCellStyle;
    }

    private CellStyle createPercentCellStyler() {
        CellStyle percentCellStyle = workbook.createCellStyle();
        percentCellStyle.setAlignment(HorizontalAlignment.CENTER);
        percentCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        percentCellStyle.setDataFormat((short) BuiltinFormats.getBuiltinFormat("0.00%"));
        percentCellStyle.setWrapText(true);
        return percentCellStyle;
    }

    @Override
    public CellStyle getStyles(boolean noneStyler, ExcelExportEntity entity) {
        if (entity != null && 10 == entity.getType()) {
            String numFormat = entity.getNumFormat();
            if (StringUtils.isNotBlank(numFormat)) {
                CellStyle cellStyle = styleMap.get(numFormat);
                return ObjectUtil.isEmpty(cellStyle) ? defaultNumberStyle : cellStyle;
            }
            return defaultNumberStyle;
        }
        return super.getStyles(noneStyler, entity);
    }
}
